// 时间
function bluetoothDataToNumber(data) {
    const reversedData = data.reverse();
    const hexString = reversedData.map(byte => byte.toString(16).padStart(2, '0')).join('');
    // const hexString = reversedData.map(byte => byte);
    const number = parseInt(hexString, 16);
    return number;
}


// id
function bluetoothDataToID(arr) {
    let str = ''
    let finalstr = ''
    if (arr[3] == 0 || arr[3] == 0x00) {
        finalstr = ''
    } else {
        finalstr = 'x'
    }
    // console.log(arr[3].toString(16), arr[2].toString(16), arr[1].toString(16), arr[0].toString(16), 'id')
    // console.log((arr[3] & 0x1f).toString(16), 'id')
    return '0x' + (arr[3] & 0x1f) + arr[2] + arr[1] + arr[0] + finalstr
    // return ('0x'+ (arr[3] & 0x1f).toString(16).toUpperCase() + arr[2].toString(16).padStart(2, '0').toUpperCase()+ arr[1].toString(16).padStart(2, '0').toUpperCase() + arr[0].toString(16).padStart(2, '0').toUpperCase() + finalstr)
}


function handleChangeLogs(arr) {
    // const arr = [0x2E, 0x60, 0x11, 0x02, 0x00, 0x00, 0x00, 0x02, 0x83, 0x1F, 0x00, 0x00, 0x23, 0x56, 0xF4, 0x26, 0x98, 0x01, 0x01, 0x00, 0xBB,]
    // const arr = [0x2E ,0x60 ,0x16,0x02,0x00, 0x00,0x00, 0xBA, 0x2E ,0x79 ,0x00 ,0x00 ,0x28 ,0x56 ,0xF4 ,0x08 ,0x98 ,0xA6 ,0x1D ,0xD0 ,0x07 ,0xE8 ,0x03 ,0x64, 0x0F ,0x1C]
    // console.log(this.hexArrayToNumber([arr[4], arr[5], arr[6], arr[7]]))
    // console.log(this.bluetoothDataToNumber([arr[8], arr[9], arr[10], arr[11]]))
    // console.log((arr[12] >> 5) & 0x03, 'CAN通道')
    // console.log(arr[12] & 0x10, '1TX ,0RX')
    // console.log(arr[12] & 0x0f, 'DLC')
    // console.log(this.bluetoothDataToID([arr[13], arr[14], arr[15], arr[16]]))
    const slicedArray = arr.slice(17, arr.length - 1);
    const time = bluetoothDataToNumber([arr[8], arr[9], arr[10], arr[11]])
    const Chl = (arr[12] >> 5) & 0x03
    const ID = bluetoothDataToID([arr[13], arr[14], arr[15], arr[16]])
    const txrx = (arr[12] & 0x10 ) == 0?'RX':'TX'
    const DLC = arr[12] & 0x0f

    let row = []
    row.push(time)
    row.push('CAN' + Chl)
    row.push(ID)
    row.push(txrx)
    row.push(DLC)
    slicedArray.forEach(el => {
        row.push(el.toString(16).padStart(2, '0').toUpperCase())
        // row.push(el.toUpperCase())
        // row.push(el)
    })
    console.log(JSON.stringify(row)+'\n\r', '每一行日志转化')
    return JSON.stringify(row)+'\n\r'
}



module.exports = {
    handleChangeLogs
}